IF OBJECT_ID (N'dbo.GetCfgCtrlKeyAsInt', N'FN') IS NOT NULL
     DROP FUNCTION dbo.GetCfgCtrlKeyAsInt
GO

CREATE FUNCTION dbo.GetCfgCtrlKeyAsInt
     ( @CfgCtrlKey varchar(30) )
RETURNS int 
AS
/*
 *
 * GetCfgCtrlKeyAsInt
 *
 * Purpose:      
 *
 * Inputs:       
 *
 * Assumes:      
 *
 * Effects:      
 *
 * Returns:      
 *
 * Note(s):      
 *
 * Author(s):    NOVA Concepts Ltd.
 *
 * Change Log:   Initial Release, 06/04/2009, 1.00.0000
 *
 */
BEGIN
     DECLARE @ReturnValue int,
             @TempValue   varchar(8000)
     
     -- Get the temp value by key
     SELECT @TempValue = CfgCtrlValue
     FROM   dbo.CfgCtrl
     WHERE  CfgCtrlKey = @CfgCtrlKey

     -- Test the value for numeric, if it is then try the convert, this COULD
     -- fail if the length is 1 and the value is something along the lines of
     -- +/-/$ and a few others, we are chosing not to test for this at this 
     -- time.
     IF ISNUMERIC( @TempValue ) = 1
     BEGIN
          SELECT @ReturnValue = CONVERT( int, @TempValue )
     END 
     -- For non-numeric @ReturnValue is already null which is the plan

     RETURN @ReturnValue
END
GO
